package com.example.controller;

import com.example.entity.User;
import com.example.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    public UserService userService;

    @RequestMapping("/getallusers")
    public ModelAndView getAllUsers(ModelAndView modelAndView, Integer page) {
        PageHelper.startPage(1, 3);
        List<User> users = userService.getAllUsers();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        if (page == null) page = 1;
        if (page <= 0) page = pageInfo.getNavigateLastPage();
        if (page > pageInfo.getNavigateLastPage()) page = pageInfo.getNavigateFirstPage();
        PageHelper.startPage(page, 3);
        users = userService.getAllUsers();
        pageInfo = new PageInfo<>(users);
        modelAndView.addObject("users", users);
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.setViewName("/alluser.jsp");
        return modelAndView;
    }

    @RequestMapping("/update")
    public ModelAndView updateById(ModelAndView modelAndView, User user) {
        if (user.getUsername().equals("")) {
            System.out.println("username:" + user.getUsername());
            modelAndView.addObject("msg", "用户为空,修改失败");
            modelAndView.setViewName("/success.jsp");
        } else {
            int ret = userService.updateById(user);
            System.out.println(ret);
            List<User> users = userService.getAllUsers();
            modelAndView.addObject("users", users);
            modelAndView.setViewName("/alluser.jsp");
        }
        return modelAndView;
    }

    @RequestMapping("/add")
    public ModelAndView addUser(ModelAndView modelAndView, User user) {
        if (user.getUsername().equals("")) {
            modelAndView.addObject("msg", "用户为空,修改失败");
            modelAndView.setViewName("/success.jsp");
        } else {
            int ret = userService.addUser(user);
            System.out.println(ret);
            List<User> users = userService.getAllUsers();
            modelAndView.addObject("users", users);
            modelAndView.setViewName("/alluser.jsp");
        }
        return modelAndView;
    }

    @RequestMapping("/getuser")
    public ModelAndView getUserByName(ModelAndView modelAndView, String username) {
        List<User> users = userService.getUserByName(username);
        modelAndView.addObject("users", users);
        modelAndView.setViewName("/getuser.jsp");
        return modelAndView;
    }

    @RequestMapping("/getusers")
    public List<User> getUsers() {
        return userService.getAllUsers();
    }


    @RequestMapping("/upload")
    public String fileUpload(MultipartFile uploadfile, Model model) throws IOException {
        String fileName = uploadfile.getOriginalFilename();
        uploadfile.transferTo(new File(fileName));
        System.out.println(fileName);
        return "index";
    }
}
